import { Strategy, StrategyOptions, ExtractJwt } from "passport-jwt"
import { PassportStrategy } from '@nestjs/passport';

import { InjectModel } from "nestjs-typegoose";
import { ModelType } from "@typegoose/typegoose/lib/types";
import { UserModel } from "src/mongodb/models/userModel/User.models";


/* token策略 */
export class jwtStrategy extends PassportStrategy(Strategy, "jwt") {
    constructor(@InjectModel(UserModel) private readonly user: ModelType<UserModel>) {

        super({
            jwtFromRequest: ExtractJwt.fromHeader('token'),
            ignoreExpiration: false,
            secretOrKey: 'PQTVSFQTUEVVCZV5BGBK'
        } as StrategyOptions)

    }

    async validate({ data }) {
        return await this.user.findOne({ name: data, status: true })
    }
}

